package cn.wolfcode.wms.domain;

import cn.wolfcode.wms.util.JSONUtil;
import cn.wolfcode.wms.util.StringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.*;
import org.apache.commons.io.FilenameUtils;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Getter@Setter@ToString
public class Product extends BaseDomain{
    private String name;
    private String sn;
    private BigDecimal costPrice;
    private BigDecimal salePrice;
    private String imagePath;
    private String intro;

    //冗余数据
    private Long brandId;
    private String brandName;

    public String getImagePath(){
        return StringUtil.empty2Null(imagePath);
    }

    public String getSmallImagePath(){
        String ext = FilenameUtils.getExtension(imagePath);
        String path = imagePath.substring(0,imagePath.lastIndexOf("."));
        return path + "_small." + ext;
    }

    //自定义属性封装成JSON
    public String getJsonString() throws JsonProcessingException {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("costPrice",costPrice);
        map.put("salePrice",salePrice);
        map.put("brandName",brandName);
        return JSONUtil.toJSONString(map);
    }
}
